package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Disease;
import com.neuedu.service.DiseaseService;
import com.neuedu.vo.DiseaseCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("疾病管理")
@RestController
@RequestMapping("/disease")
@CrossOrigin
public class DiseaseController {

    @Autowired
    private DiseaseService diseaseService;

    @ApiOperation("疾病列表")
    @GetMapping("/list")
    public ServerResponse list(DiseaseCondition diseaseCondition) {

        //调用业务方法
        Page<Disease> diseasePage = diseaseService.getDiseaseList(diseaseCondition);

        //返回分页对象
        return ServerResponse.success("").data("diseasePage", diseasePage);
    }

    @ApiOperation("添加疾病")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Disease disease) {

        //服务器端数据校验---疾病名称
        if(StringUtils.isEmpty(disease.getDiseaseName()))  {
            return ServerResponse.error("疾病名称不能为空");
        }

        if(disease.getDiseaseName().length()>10){
            return ServerResponse.error("疾病名称的长度不能超过10个字符");
        }

        //服务器端数据校验---疾病编码
        if(StringUtils.isEmpty(disease.getDiseaseCode()))  {
            return ServerResponse.error("疾病编码不能为空");
        }

        if(disease.getDiseaseCode().length()>10){
            return ServerResponse.error("疾病编码的长度不能超过10个字符");
        }

        //添加操作
        if(diseaseService.save(disease)){
            return ServerResponse.success("添加疾病成功");
        }else{
            return ServerResponse.error("添加疾病失败");
        }
    }

    @ApiOperation("根据编号获取疾病")
    @GetMapping("/get/{diseaseId}")
    public ServerResponse get(@PathVariable int diseaseId) {

        Disease disease = diseaseService.getById(diseaseId);

        if(disease!=null){
            return ServerResponse.success("").data("disease", disease);
        }else{
            return ServerResponse.error("获取疾病信息失败");
        }
    }

    @ApiOperation("修改疾病")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Disease disease) {

        //服务器端数据校验---疾病名称
        if(StringUtils.isEmpty(disease.getDiseaseName()))  {
            return ServerResponse.error("疾病名称不能为空");
        }

        if(disease.getDiseaseName().length()>10){
            return ServerResponse.error("疾病名称的长度不能超过10个字符");
        }

        //服务器端数据校验---疾病编码
        if(StringUtils.isEmpty(disease.getDiseaseCode()))  {
            return ServerResponse.error("疾病编码不能为空");
        }

        if(disease.getDiseaseCode().length()>10){
            return ServerResponse.error("疾病编码的长度不能超过10个字符");
        }

        //修改操作
        if(diseaseService.updateById(disease)){
            return ServerResponse.success("修改疾病成功");
        }else{
            return ServerResponse.error("修改疾病失败");
        }
    }

    @ApiOperation("删除疾病")
    @DeleteMapping("/remove/{diseaseId}")
    public ServerResponse remove(@PathVariable int diseaseId) {

        if(diseaseService.removeById(diseaseId)){
            return ServerResponse.success("删除疾病成功");
        }else{
            return ServerResponse.error("删除疾病失败");
        }
    }

    @ApiOperation("判断指定的疾病是否可以删除")
    @GetMapping("/removeEnable/{diseaseId}")
    public ServerResponse removeEnable(@PathVariable int diseaseId) {

        if(diseaseService.removeEnable(diseaseId)){
            return ServerResponse.success("允许删除疾病");
        }else{
            return ServerResponse.error("当前疾病下存在挂号，不能删除");
        }
    }

    @ApiOperation("批量删除疾病")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String diseaseIds) {

        if(diseaseService.deleteBatch(diseaseIds)){
            return ServerResponse.success("批量删除疾病成功");
        }else{
            return ServerResponse.error("批量删除疾病成功失败");
        }
    }

    @ApiOperation("疾病列表---确诊病历时使用")
    @GetMapping("/listAll")
    public ServerResponse list() {

        List<Disease> diseaseList = diseaseService.list();

        return ServerResponse.success("").data("diseaseList", diseaseList);
    }
}